<?php
namespace app\api\controller\v1\member;
use app\api\controller\v1\Init;

class Member extends Init
{
	public function _initialize() {
		parent::_initialize();
		$this->model = model('member/member', 'service');
	}

	/* 成员列表 */
	public function lists(){
		$maps = [];
		if($this->params['keyword']) {
			$maps['nickname|province|city|country'] = ['like', '%'.$this->params['keyword'].'%'];
		}
		$info = $this->model->lists($maps, $this->params['limit'], $this->params['page']);
		foreach ($info as $key => $value) {
			//关联模型查询
			if($this->params['isfans'] === true) $value->fans;
			if($this->params['islive'] === true) $value->live;
		}
		return $this->response(lang('Operation completed'), $info);
	}

	/* 成员详情 */
	public function info() {
		$maps = [];
		$maps['id'] = $this->params['id'];
		$info = $this->model->get($maps);
		//关联模型查询
		if($this->params['isfans'] === true) $info->fans;
		if($this->params['islive'] === true) $info->live;
		return $this->response(lang('Operation completed'), $info);
	}

	//根据token获取用户信息
    public function get_user_info(){
    	$token = $this->params['token'];
		$result = $this->model->get_user_info($token);
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
    }

	/* 成员添加 */
	public function add() {
		$post = $this->request->post();
		$result = $this->model->add($post, [], 'add');
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	/* 成员编辑 */
	public function edit() {
		$post = $this->request->post();
		$result = $this->model->edit($post, ['id' => $post['id']], 'edit');
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	/* 成员删除 */
	public function delete() {
		$id = $this->request->post('id/s');
		$ids = explode(",", $id);
		$maps = ['id' => ['in', $ids]];
		$result = $this->model->where($maps)->delete();
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	/* 拉黑&移除 */
	public function blacklist(){
		$result = $this->model->blacklist($this->params['id']);
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	/* 根据code码换取用户信息 */
	public function code(){
		$result = $this->model->code($this->params['code'],$this->params['identify']);
		if($result === false) {
			$url = url('/member/member/auth/auth',['identify'=>$this->params['identify']],false,true);
			return $this->response($this->model->getError(), ['redirect_url'=> $url], -30001);
		}
		return $this->response(lang('Operation completed'), $result);
	}
}